<?php if (!defined('CORE_DIR')) die('Wrong entry point!');

/* 
 * Copyright 2010 Anton Muraviev a.k.a kuromaisu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */


Aero::define('EnglishGBLocale', AS_SINGLETON);

/* -----------------------------------------------------------------------------
 * Class: EnglishGBLocale
 * -----------------------------------------------------------------------------
 */
class EnglishGBLocale extends AeroLocale
{
	protected $magnitudes = array(
		'thousand',
		'million',
		'billion',
		'trillion',
		'quadrillion',
		'quintillion',
		'sextillion',
		'septillion',
		'octillion',
		'nonillion',
		'decillion',
		'undecillion',
		'duodecillion',
		'tredecillion'
	);
	
	protected $numbers = array(
		0 => '',
		1 => 'one',
		2 => 'two',
		3 => 'three',
		4 => 'four',
		5 => 'five',
		6 => 'six',
		7 => 'seven',
		8 => 'eight',
		9 => 'nine',
		10 => 'ten',
		11 => 'eleven',
		12 => 'twelve',
		13 => 'thirteen',
		14 => 'fourteen',
		15 => 'fifteen',
		16 => 'sixteen',
		17 => 'seventeen',
		18 => 'eighteen',
		19 => 'nineteen'
	);
	
	protected $decimals = array(
		0 => '',
		2 => 'twenty',
		3 => 'thirty',
		4 => 'fourty',
		5 => 'fifty',
		6 => 'sixty',
		7 => 'seventy',
		8 => 'eighty',
		9 => 'ninety',
	);
	
	public function spellout($number)
	{
		if (!is_numeric($number) || empty($number))
		{
			return 'zero';
		}
		
		$string = '';
		$result = $this->_spellout($number);
		
		if (count($result) > 1)
		{
			$string = ' and ' . array_pop($result);
		}
		
		return implode(' ',$result) . $string;
	}
	
	protected function _spelloutMagnitude($integer, $power)
	{
		if (isset($this->magnitudes[$power-1]))
		{
			return ' '.$this->magnitudes[$power-1];
		}
		
		return '';
	}
	
	protected function _spelloutHundreds($hundred)
	{
		if ($hundred > 0)
		{
			return $this->numbers[$hundred] . ' hundred';
		}
	}

	protected function _spelloutDecimals($decimal)
	{
		$string = '';
		
		$a10 = floor($decimal/10);
		$u10 = $decimal % 10;
		
		if ($a10 >= 2)
		{
			$string.= $this->decimals[$a10];
			
			if ($u10 > 0)
			{
				$string.= ' ' . $this->numbers[$u10];
			}
		}
		else if ($u10 > 0 || $a10 > 0)
		{
			$string.= $this->numbers[$a10 * 10 + $u10];
		}
		
		return $string;
	}
}
